﻿using System;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell.Interop;
using MsVsShell = Microsoft.VisualStudio.Shell;
using ErrorHandler = Microsoft.VisualStudio.ErrorHandler;
using DslShell = global::Microsoft.VisualStudio.Modeling.Shell;

namespace devtm.AutoMapper
{

    //[MsVsShell.ProvideToolWindow(typeof(PersistedWindowPane), Style = MsVsShell.VsDockStyle.Tabbed, Orientation = Microsoft.VisualStudio.Shell.ToolWindowOrientation.Bottom, Window = "3ae79031-e1bc-11d0-8f78-00a0c9110057")]
    [MsVsShell::ProvideToolWindowVisibility(typeof(AutomapperDetailsToolWindows), Constants.AutoMapperEditorFactoryId)]
    [MsVsShell::ProvideToolWindow(typeof(AutomapperDetailsToolWindows), MultiInstances = false, Style = MsVsShell::VsDockStyle.Tabbed, Orientation = MsVsShell::ToolWindowOrientation.Right, Window = "{3AE79031-E1BC-11D0-8F78-00A0C9110057}")]
    internal sealed partial class AutoMapperPackage 
    {



        // Cache the Menu Command Service since we will use it multiple times
        private MsVsShell.OleMenuCommandService menuService;

        /// <summary>
        /// Package contructor.
        /// While we could have used the default constructor, adding the Trace makes it
        /// possible to verify that the package was created without having to set a break
        /// point in the debugger.
        /// </summary>
        public AutoMapperPackage()
        {
            Trace.WriteLine(String.Format(CultureInfo.CurrentCulture, "Entering constructor for class {0}.", this.GetType().Name));
        }




        /// <summary>
        /// Initialization of the package; this is the place where you can put all the initilaization
        /// code that rely on services provided by VisualStudio.
        /// </summary>
        protected override void Initialize()
        {


            this.AddToolWindow(typeof(AutomapperDetailsToolWindows));


            Trace.WriteLine(String.Format(CultureInfo.CurrentCulture, "Entering Initialize for class {0}.", this.GetType().Name));
            base.Initialize();

            // Create one object derived from MenuCommand for each command defined in
            // the VSCT file and add it to the command service.

            // Each command is uniquely identified by a Guid/integer pair.
            CommandID id = new CommandID(GuidsList.GuidAutoMapperShowDetails, GuidsList.cmdidPersistedWindow);
            // Add the handler for the persisted window with selection tracking
            DefineCommandHandler(new EventHandler(this.ShowPersistedWindow), id);

        }

        /// <summary>
        /// Define a command handler.
        /// When the user press the button corresponding to the CommandID
        /// the EventHandler will be called.
        /// </summary>
        /// <param name="id">The CommandID (Guid/ID pair) as defined in the .vsct file</param>
        /// <param name="handler">Method that should be called to implement the command</param>
        /// <returns>The menu command. This can be used to set parameter such as the default visibility once the package is loaded</returns>
        internal MsVsShell.OleMenuCommand DefineCommandHandler(EventHandler handler, CommandID id)
        {
            // if the package is zombied, we don't want to add commands
            if (this.Zombied)
                return null;

            // Make sure we have the service
            if (menuService == null)
            {
                // Get the OleCommandService object provided by the MPF; this object is the one
                // responsible for handling the collection of commands implemented by the package.
                menuService = GetService(typeof(IMenuCommandService)) as MsVsShell.OleMenuCommandService;
            }
            MsVsShell.OleMenuCommand command = null;
            if (null != menuService)
            {
                // Add the command handler
                command = new MsVsShell.OleMenuCommand(handler, id);
                menuService.AddCommand(command);
            }
            return command;
        }

        /// <summary>
        /// This method loads a localized string based on the specified resource.
        /// </summary>
        /// <param name="resourceName">Resource to load</param>
        /// <returns>String loaded for the specified resource</returns>
        internal string GetResourceString(string resourceName)
        {
            string resourceValue;
            IVsResourceManager resourceManager = (IVsResourceManager)GetService(typeof(SVsResourceManager));
            if (resourceManager == null)
            {
                throw new InvalidOperationException("Could not get SVsResourceManager service. Make sure the package is Sited before calling this method");
            }
            Guid packageGuid = this.GetType().GUID;
            int hr = resourceManager.LoadResourceString(ref packageGuid, -1, resourceName, out resourceValue);
            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr);
            return resourceValue;
        }

        /// <summary>
        /// Event handler for our menu item.
        /// This results in the tool window being shown.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="arguments"></param>
        private void ShowPersistedWindow(object sender, EventArgs arguments)
        {

            AutomapperDetailsToolWindows details = this.AutoMapperViewDetails;

            if (details != null)
                details.Show();

        }


        /// <summary>
        /// Returns the explorer tool window.
        /// </summary>
        private AutomapperDetailsToolWindows AutoMapperViewDetails
        {
            get
            {

                AutomapperDetailsToolWindows explorerWindow =
                    explorerWindow = this.GetToolWindow(typeof(AutomapperDetailsToolWindows), true) as AutomapperDetailsToolWindows;

                return explorerWindow;

            }
        }


    }
}
